<script setup>
import { computed } from 'vue';

const props = defineProps({
  list: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['changeFn', 'del', 'changeAll'])

const isCheckAll = computed({
  get() {
    // every 判断每一项都符合条件，就会返回 true
    // 对于空数组，也会返回 true

    // some 判断有一项不符合条件，就返回 false
    return props.list.every(v => v.done) && props.list.length > 0
  },
  set(bl) {
    console.log('bl', bl);
    emit('changeAll', bl)
  }
})
</script>

<template>
  <section class="main">
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isCheckAll" />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li :class="{ completed: item.done }" v-for="item in list" :key="item.id">
        <div class="view">
          <input class="toggle" type="checkbox" :checked="item.done" @change="emit('changeFn', item.id)" />
          <label>{{ item.name }}</label>
          <button class="destroy" @click="emit('del', item.id)"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
    </ul>
  </section>
</template>

<style lang="less" scoped>
</style>
